package cn.igroup.service.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {

    /**
     * 用户名
     */
    public static final String REGEX_USERNAME = "^[A-Za-z0-9_]{4,}+$";//^[A-Za-z]([A-Za-z0-9_]{4,})+$
    /**
     * 手机号码
     */
    public static final String REGEX_PHONE = "^1(3|4|5|7|8)[0-9]\\d{8}$";
    /**
     * EMAIL
     */
    public static final String REGEX_EMAIL = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
    /**
     * 域名
     */
    public static final String REGEX_DOMAIN = "^([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
    /**
     * 身份证
     */
    public static final String REGEX_ID_CARD_NUMBER = "^(\\d{6})(18|19|20)?(\\d{2})([01]\\d)([0123]\\d)(\\d{3})(\\d|X|x)?$";
    /**
     * 中文
     */
    public static final String REGEX_ZH_CN = "^[\\u4e00-\\u9fa5]{0,}$";
    /**
     * IP
     */
    public static final String REGEX_IP = "^((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5]|[*])$";
    //                                     ^(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)$
    /**
     * 电话号码
     */
    public static final String REGEX_TEL = "\\d{3}-\\d{8}|\\d{4}-\\d{7}";
    /**
     * 日期
     */
    public static final String REGEX_DATE = "^[1-9]\\d{3}([-|\\/|\\.])?((0\\d)|([1-9])|(1[0-2]))\\1(([0|1|2]\\d)|([1-9])|3[0-1])$/";

    /**
     * 获得字符串类型
     *
     * @param str
     * @return int
     */
    public static int regex(String str) {
        Pattern pattern = Pattern.compile(REGEX_PHONE);
        Matcher matcher = pattern.matcher(str);
        if (matcher.matches()) {
            return 1;
        }

        pattern = Pattern.compile(REGEX_EMAIL);
        matcher = pattern.matcher(str);
        if (matcher.matches()) {
            return 2;
        }

        pattern = Pattern.compile(REGEX_USERNAME);
        matcher = pattern.matcher(str);
        if (matcher.matches()) {
            return 3;
        }

        return 0;
    }

    /**
     * 匹配验证字符串
     *
     * @param str 要验证的类型
     * @return Boolean
     */
    public static Boolean regex(String str, String expression) {
        Pattern pattern = Pattern.compile(expression);
        return pattern.matcher(str).matches();
    }

}
